package com.social.controller;

import com.alibaba.fastjson.JSON;
import com.social.pojo.Organization;
import com.social.pojo.Post;
import com.social.pojo.PostOrganization;
import com.social.service.OrganizationService;
import com.social.service.PostOrganizationService;
import com.social.service.PostService;
import com.social.util.JsonUtil;
import com.social.util.PageUtil;
import com.social.util.StatusUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpSession;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 组织机构管理
 */
@Controller
@RequestMapping("Organization")
public class OrganizationController {
    /**
     * Organization 业务层对象
     */
    @Autowired
    private OrganizationService organizationService;
    /**
     * PostOrganization 业务层对象
     */
    @Autowired
    private PostOrganizationService postOrganizationService;

    /**
     * 分页查所有
     *
     * @param page    当前页数
     * @param pageNum 每页显示条数
     * @param session 作用域
     * @return 页面跳转状态
     */
    @RequestMapping("selectAll")
    public String selectAll(Integer page, Integer pageNum, HttpSession session, Integer status) {
        List<Organization> organizations = organizationService.selectAll(page, pageNum);
        int count = organizationService.selectAllCount();
        //分页方法
        Map<String, Integer> util = PageUtil.page(pageNum, page, count);
        Integer pages = util.get("page");
        Integer remaining_pages = util.get("remaining_pages");
        Integer counts = util.get("count");
        Map<String, Object> map = new HashMap<String, Object>();
        //存入分页数据
        map.put("num", remaining_pages);
        map.put("page", pages);
        map.put("count", counts);
        //存入map集合
        session.setAttribute("map", map);
        session.setAttribute("organizations", organizations);
        return StatusUtil.statusYe(status);
    }

    /**
     * 添加
     *
     * @param departmentName 部门名称
     * @param id             上级部门id
     * @return 添加状态
     */
    @RequestMapping(value = "insert", produces = "text/application;charset=utf-8")
    @ResponseBody
    public String insert(String departmentName, Integer id) {
        JsonUtil jsonUtil = new JsonUtil();
        //根据部门名称查询
        Organization organizations = organizationService.selectByUsername(departmentName);
        //状态
        int status = 0;
        //查询数据为空执行
        if (organizations == null) {
            Organization organization = new Organization(null, departmentName, id);
            //添加操作
            status = organizationService.insertSelective(organization);
            //根据部门名称查询
            Organization organizationss = organizationService.selectByUsername(departmentName);
            PostOrganization postOrganization = new PostOrganization(id, organizationss.getId());
            //添加操作
            postOrganizationService.insert(postOrganization);
        } else {
            jsonUtil.setCode("部门名称重复");
            return JSON.toJSONString(jsonUtil.getCode());
        }
        Boolean restates = jsonUtil.getRestates(status);
        return JSON.toJSONString(restates);
    }

    /**
     * 根据organization primary key 删除
     *
     * @param id organization primary key
     * @return 删除状态
     */
    @RequestMapping("delete")
    @ResponseBody
    public String delete(Integer id) {
        int status = organizationService.deleteByPrimaryKey(id);
        JsonUtil jsonUtil = new JsonUtil();
        //设置状态
        Boolean restates = jsonUtil.getRestates(status);
        return JSON.toJSONString(restates);
    }

    /**
     * 根据organization primary key 查询
     *
     * @param id organization primary key
     * @return 查询数据
     */
    @RequestMapping(value = "selectById", produces = "text/application;charset=utf-8")
    @ResponseBody
    public String selectById(Integer id) {
        Organization organization = organizationService.selectByPrimaryKey(id);
        return JSON.toJSONString(organization);
    }

    /**
     * 根据  organization primary key 修改
     *
     * @param id             organization primary key
     * @param departmentName 部门名称
     * @param postId         岗位id
     * @return 修改状态
     */
    @RequestMapping("update")
    @ResponseBody
    public String update(Integer id, String departmentName, Integer postId) {
        Organization organization = new Organization(id, departmentName, postId);
        //修改操作
        int status = organizationService.updateByPrimaryKey(organization);
        JsonUtil jsonUtil = new JsonUtil();
        //设置状态
        Boolean restates = jsonUtil.getRestates(status);
        return JSON.toJSONString(restates);
    }
}